package com.changgou.goods.service.myimpl;

import com.changgou.goods.dao.mydao.BrandMapper_test;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import java.util.List;
import java.util.Map;

@Service("brandServiceImpl_test2")
public class BrandServiceImpl_test2 implements BrandService {

    @Autowired
    private BrandMapper_test mapper;


    @Override
    public List<Brand> findAll() {
        return mapper.selectAll();
    }

    @Override
    public Brand findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Brand brand) {
        mapper.insertSelective(brand);
    }

    @Override
    public void update(Brand brand) {
        mapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void delete(Integer id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);

        return mapper.selectByExample(example);
    }

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);

        Example.Criteria criteria = example.createCriteria();

        if (searchMap != null) {
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
                criteria.andLike("image", "%" + searchMap.get("image") + "%");
            }
            // 品牌的首字母
            if (searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))) {
                criteria.andLike("letter", "%" + searchMap.get("letter") + "%");
            }

            // 品牌id
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 排序
            if (searchMap.get("seq") != null) {
                criteria.andEqualTo("seq", searchMap.get("seq"));
            }
        }
        return example;
    }

    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Brand>)mapper.selectAll();
    }

    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {

        PageHelper.startPage(page,size);

        Example example = createExample(searchMap);
        return (Page<Brand>) mapper.selectByExample(example);
    }

    // 通过种类名称查询品牌列表
    @Override
    public List<Map> findBrandListByCategoryName(String categoryName) {
        return mapper.findBrandListByCategoryName(categoryName);

    }
}
